package com.example.controller;

import com.example.common.Result;
import com.example.dto.DeptDTO;
import com.example.entity.Dept;
import com.example.service.DeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@RestController
@RequestMapping("/depts")
@Tag(name = "部门管理", description = "部门信息的增删改查")
public class DeptController {
    
    @Autowired
    private DeptService deptService;
    
    /**
     * 查询所有部门
     */
    @GetMapping
    @Operation(summary = "查询所有部门", description = "获取所有部门信息")
    public Result<List<Dept>> list() {
        List<Dept> list = deptService.list();
        return Result.success(list);
    }
    
    /**
     * 根据ID查询部门
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据ID查询部门", description = "根据部门ID查询部门详细信息")
    @Parameter(name = "id", description = "部门ID", required = true, example = "1")
    public Result<Dept> getById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }
    
    /**
     * 新增部门
     */
    @PostMapping
    @Operation(summary = "新增部门", description = "添加新部门信息")
    public Result<String> save(@RequestBody DeptDTO deptDTO) {
        deptService.save(deptDTO);
        return Result.success();
    }
    
    /**
     * 修改部门
     */
    @PutMapping
    @Operation(summary = "修改部门", description = "修改部门信息")
    public Result<String> update(@RequestBody DeptDTO deptDTO) {
        deptService.update(deptDTO);
        return Result.success();
    }
    
    /**
     * 删除部门
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除部门", description = "根据ID删除部门")
    @Parameter(name = "id", description = "部门ID", required = true, example = "1")
    public Result<String> delete(@PathVariable Integer id) {
        deptService.delete(id);
        return Result.success();
    }
} 